const mongoose = require('./database')

// 用户信息
const user_Shop = new mongoose.Schema({
    username: { // 账号
        type: String,
        required: true,
        unique: true
    },
    password: { // 密码
        type: String,
        required: true
    },
    createdAt: { // 注册时间
        type: Date,
        default: Date.now
    },
    img: {   //头像
        type: String,
        default: null,
    },
    phone: { // 联系电话
        type: String,
        required: true
    },
    name:{
        type:String,
        default:null
    },
    sex:{
        type:String,
        enum:['男','女'],
        default:null
    },
    birth:{
        type:Date,
        default:null
    },
    zfpwd:{
        type:String,
        default:null
    }
})
const user = mongoose.model('user', user_Shop, 'user')


// 商品信息
const Shops_Shop = new mongoose.Schema({
    name: { // 商品名称
        type: String,
        required: true
    },
    price: { // 价格
        type: Number,
        required: true
    },
    description: { // 商品描述
        type: String
    },
    createdAt: { // 上架时间
        type: Date,
        default: Date.now
    },
    colorx: [{   //商品颜色
        context: {
            type: String,
        }
    }],
    chima: [{   //商品尺寸
        context: {
            type: String
        }
    }],
    // 商品图片
    images:{
        type: String,
        required: true
    },
    // 新增分类字段
    category: { 
        type: String,
        required: true,
    }
})
const shops = mongoose.model('shops', Shops_Shop, 'shops')


// 商品详情页
const shop_Shop = new mongoose.Schema({
    shopId: {  //商品信息
        type: mongoose.Schema.Types.ObjectId,
        ref: 'shops',
        default: mongoose.Types.ObjectId
    },
    details: { // 详情内容
        type: String
    },
    images: [{ // 图片数组
        type: String
    }]
})
const shop = mongoose.model('shop', shop_Shop, 'shop')


// 消息
const Message_Shop = new mongoose.Schema({
    userId: { // 用户ID
        type: mongoose.Schema.Types.ObjectId,
        ref: 'user'
    },
    content: { // 消息内容
        type: String,
        required: true
    },
    createdAt: { // 发送时间
        type: Date,
        default: Date.now
    },
    read: { // 是否已读
        type: Boolean,
        default: false
    }
})
const message = mongoose.model('message', Message_Shop, 'message')


// 购物车信息
const gouwu_Shop = new mongoose.Schema({
    userId: { // 用户ID
        type: mongoose.Schema.Types.ObjectId,
        ref: 'user'
    },
    shopId: { // 商品ID
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref:"shops"
    },
    num:{ // 数量
        type: Number,
        required: true,
        default: 1
    },
    colorx:{//颜色
        type:String,
        required:true,
    },
    chima:{//尺码
        type:String,
        required:true
    }
})
const gouwu = mongoose.model('gouwu', gouwu_Shop, 'gouwu')

// 住址
const Address_Shop = new mongoose.Schema({
    userId: { // 用户ID
        type: mongoose.Schema.Types.ObjectId,
        ref: 'user'
    },
    name: { // 收件人姓名
        type: String,
        required: true
    },
    phone: { // 收件人电话
        type: String,
        required: true
    },
    area: {// 省市区
        type: String,
        required: true
    },
    areaCode: { // 省市区编码
        type: String,
        required: true
    },
    address: { // 详细地址
        type: String,
        required: true
    },
    isDefault: { // 是否默认地址
        type: Boolean,
        default: false
    }
})
const address = mongoose.model('address', Address_Shop, 'address')





// 订单
const dingdan_shop = new mongoose.Schema({
    userId: { // 用户ID
        type: mongoose.Schema.Types.ObjectId,
        ref: 'user'
    },
    shopsId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'shops'
    },
    colorx:String, //商品颜色
    chima:String,   //商品尺寸
    num:Number, //数量
    cart:{   //订单状态
        type:String,
        default:'A'
    },
    orderNo: { // 新增订单号字段
        type: String,
        required: true
    }
})
const dingdan  = mongoose.model('dingdan',dingdan_shop,'dingdan')


module.exports = {
    user,
    shops,
    shop,
    message,
    gouwu,
    address,
    dingdan
}